﻿<span class="bold underline large">Write the method highestVal that will find the highest value in a two-dimensional array of integers. Below is a method header:</span>
<span class="code">
    public static int highestVal(int[][] arr) {

    }
</span>

<span class="bold">First, create an integer to store the maximum value that will be returned at the end of the method.</span>
<span class="italic">Note: The variable must be initialized so that in later steps comparisons can occur without compiler errors.</span>
<span class="code">
    public static int highestVal(int[][] arr) {
    <span class="newCode">
        int max = 0;
    </span>
    }
</span>

<span class="bold">Next, create a nested for loop that will iterate through all elements of the array. This will require the use of two for loops, one that iterates through all of the rows, the second that iterates through all of the columns. </span>
<span class="italic">Note: Using arr[0].length in the second for loop will work assuming that the array is a rectangular array. In order to make this work for jagged arrays you should use arr[i].length</span>
<span class="code">
    public static int highestVal(int[][] arr) {
        int max = 0;
    <span class="newCode">
        for(int i = 0; i &#60 arr.length; i++) {
            for(int j = 0; j &#60 arr[0].length; j++) {

            }
        }
    </span>
    }
</span>

<span class="bold">Now, within nested loops create a comparison. This comparison should check and see if the current array element is larger than the current maximum.</span>
<span class="italic">Consider: Does it make a difference if &#62;= is used instead of &#62 in this problem?</span>
<span class="code">
    public static int highestVal(int[][] arr) {
        int max = 0;

        for(int i = 0; i &#60 arr.length; i++) {
            for(int j = 0; j &#60 arr[0].length; j++) {
                <span class="newCode">if(arr[i][j] &#62 max) {

                }</span>
            }
        }
    }
</span>

<span class="bold">Once the comparison is made, determine what needs to be done if the current array element is smaller or larger. If it is smaller, there is no need to do anything, but if it is larger, save the element within the max variable.</span>
<span class="code">
    public static int highestVal(int[][] arr) {
        int max = 0;

        for(int i = 0; i &#60 arr.length; i++) {
            for(int j = 0; j &#60 arr[0].length; j++) {
                if(arr[i][j] &#62 max) {
                    <span class="newCode">max = arr[i][j];</span>    
                }
            }
        }
    }
</span>

<span class="bold">At this point, the nested for loops should find the highest value, so now you need to return the highest value to complete the challenge. This return statement should be placed outside the nested for loops, and at the very end of the method. </span>
<span class="code">
    public static int highestVal(int[][] arr) {
        int max = 0;

        for(int i = 0; i &#60 arr.length; i++) {
            for(int j = 0; j &#60 arr[0].length; j++) {
                if(arr[i][j] &#62 max) {
                    max = arr[i][j];
                }
            }
        }
        <span class="newCode">return max;</span>            
    }
</span>

<span class="bold">Below we will supply a class for you to insert your method in to. The output that should be printed into the console for the supplied two-dimensional array should be 10. If you want to confirm that the code works for all instances, change various numbers and the placement of the highest value. </span>
<span class="italic">Note: This method should be able to work for any two-dimensional array of integers</span>
<span class="italic">Consider: Can this method be made more efficient?</span>
<span class="code">
    public class exampleProblem {
        public static void main(String[] args) {
            int[][] arr = new int[][]{
                {9,7,4,6},
                {1,2,11,7},
                {2,4,6,10}};

            System.out.println(highestVal(arr));
        }
        
        <span class="highlight">//Insert your highestVal method here</span>

    }
</span>
<span class="bold">Change the values in the array along with the size of the array to continue checking that you get the correct result</span>
<br />
<span class="italic">Additional challenge: Try returning the indices of the last instance of the maximum integer, along with the value (You will need to create a new object for this)</span>

<a href="#exampleTop">Back to top</a>